<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
<ui:composition>

    <p:outputLabel value="Link to download file: "/>
    <p:commandLink value="#{documentsBean.document.getLink()}" action="#{navigationBean.redirect()}"/>
    <br/>
    <h:form>
        <p:messages id="emailMes" showDetail="true" autoUpdate="true" closable="true"/>
        <h:panelGrid columns="3">
            <p:outputLabel value="Send link to a friend"/>
            <p:inputText alt="test" value="#{sendLinkBean.email}" required="true">
                <f:ajax event="keyup" execute="@this" render="emailMes"/>
                <f:validateRegex pattern=".+@.+\..+"/>
            </p:inputText>
            <p:commandButton value="Send" actionListener="#{sendLinkBean.sendLink}"/>
        </h:panelGrid>
    </h:form>

    <br/>

    <p:dataTable value="#{documentsBean.document}">
        <f:facet name="header">#{documentsBean.document.documentName}</f:facet>

        <p:column headerText="Description">
            <f:facet name="header">Description</f:facet>
            <p:outputLabel value="#{documentsBean.document.description}"
                           rendered="#{not empty documentsBean.document.description}"/>
            <p:outputLabel value="Without description" rendered="#{empty documentsBean.document.description}"/>
        </p:column>

        <p:column headerText="Upload date">
            <f:facet name="header">Upload date</f:facet>
            <p:outputLabel value="#{documentsBean.document.date}">
                <f:convertDateTime pattern="dd.MM.yyyy HH:mm"/>
            </p:outputLabel>
        </p:column>

        <p:column headerText="Delete date">
            <f:facet name="header">Delete date</f:facet>
            <p:outputLabel value="#{documentsBean.document.deleteDate}"
                           rendered="#{documentsBean.document.deleteDate != null}">
                <f:convertDateTime pattern="dd.MM.yyyy HH:mm"/>
            </p:outputLabel>
            <p:outputLabel value="no deleted" rendered="#{documentsBean.document.deleteDate == null}"/>
        </p:column>

        <p:column headerText="Permissions">
            <f:facet name="header">Permissions</f:facet>
            <p:outputLabel value="Only for me" rendered="#{documentsBean.document.permissions == 1}"/>
            <p:outputLabel value="For all users" rendered="#{documentsBean.document.permissions == 0}"/>
        </p:column>

        <p:column headerText="Size (Kb)">
            <f:facet name="header">Size (Kb)</f:facet>
            <p:outputLabel value="#{documentsBean.document.size}"/>
        </p:column>
    </p:dataTable>
    <h:form>
        <p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s">
            <p:fileDownload value="#{documentsBean.file}"/>
        </p:commandButton>
        <p:commandButton value="Edit" actionListener="#{navigationBean.action('edit.xhtml')}"
                         rendered="#{(userBean.user.idUser != 0 and
                         userBean.user.idUser == documentsBean.document.user.idUser) or
                         userBean.user.role.idRole == 3}"/>
        <p:commandButton value="Remove" actionListener="#{documentsBean.delete}" rendered="#{(userBean.user.idUser != 0 and
                         userBean.user.idUser == documentsBean.document.user.idUser) or
                         userBean.user.role.idRole == 3}">
            <p:confirm header="Delete document" message="Are you sure?" icon="ui-icon-alert" />
        </p:commandButton>
        <p:confirmDialog global="true">
            <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
            <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
        </p:confirmDialog>


    </h:form>


    <p:separator/>
    <h:form rendered="#{userBean.user.role.idRole > 1}">
        <p:panel header="Comment">
            <p:outputLabel value="You can leave a comment about the document"/>
            <br/>
            <h:panelGrid columns="2">
                <p:inputTextarea value="#{commentBean.comment.body}"/>
                <p:commandButton type="submit" value="Ok" action="#{commentBean.sendComment}"/>
            </h:panelGrid>
        </p:panel>
        <p:separator/>
    </h:form>

    <p:dataList value="#{commentBean.comments}" var="c" type="definition" rendered="#{not empty commentBean.comments}">
        <f:facet name="header">Commentst</f:facet>
        <h:panelGrid columns="3">
        <p:outputLabel value="#{c.user.login}: "/>
        <p:outputLabel value="#{c.body}" />
        <p:commandLink actionListener="#{commentBean.deleteComment()}" value="delete" rendered="#{userBean.user.role.idRole == 3}">
            <p:confirm header="Delete document" message="Are you sure?" icon="ui-icon-alert" />
            <p:ajax event="click" listener="#{commentBean.setComment(c)}"/>
        </p:commandLink>
        <p:confirmDialog global="true">
            <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
            <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
        </p:confirmDialog>
        </h:panelGrid>
    </p:dataList>
    <p:outputLabel value="Nobody has posted a comment to this file" rendered="#{empty commentBean.comments}"/>
</ui:composition>
</html>
